package com.examsage.examsage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.examsage.examsage.interceptor.LoginInterceptor;
import com.examsage.examsage.model.SystemUser;
import com.examsage.examsage.mapper.SystemUserMapper;
import com.examsage.examsage.model.vo.ResponseVO;
import com.examsage.examsage.service.ISystemUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.examsage.examsage.utils.JWTUtil;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author examsage
 * @since 2025-07-02
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser> implements ISystemUserService {

    @Resource
    private SystemUserMapper systemUserMapper;

    @Override
    public ResponseVO login(SystemUser systemUser) {
        // TODO: Implement login logic
        List<SystemUser> systemUsers = systemUserMapper.selectList(new QueryWrapper<SystemUser>().eq("username", systemUser.getUsername()));
        ResponseVO responseVO = new ResponseVO();
        if(systemUsers != null && systemUsers.size() == 1){
            SystemUser user = systemUsers.get(0);
            if(user.getUsername().equals(systemUser.getUsername()) && systemUser.getPassword().equals(user.getPassword())) {// 登录成功
                String token = JWTUtil.geneJsonWebToken(user);; // TODO: Implement token generation logic
                responseVO.setStatusCode(200);
                responseVO.setMessage("登录成功");
                responseVO.setData(Map.of(
                        "user", user,
                        "token", token
                ));
                return responseVO;
            }
        }
        responseVO.setStatusCode(401);
        responseVO.setMessage("登录失败");
        responseVO.setData(null);
        return responseVO; // 用户不存在
    }

    @Override
    public ResponseVO register(SystemUser systemUser) {
         // TODO: Implement register logic
        systemUser.setStatus(true);
        systemUser.setGmtCreate(java.time.LocalDateTime.now());
        systemUser.setGmtModified(java.time.LocalDateTime.now());
        ResponseVO responseVO = new ResponseVO();
        if(systemUserMapper.insert(systemUser) > 0){
            responseVO.setStatusCode(201);
            responseVO.setMessage("注册成功");
            responseVO.setData(systemUser);
        } else {
            responseVO.setStatusCode(401);
            responseVO.setMessage("注册失败");
            responseVO.setData(null);
        }
        return responseVO; // 注册失败
    }

    @Override
    public ResponseVO getCurrentUserMsg() {
        // TODO: Implement getCurrentUser logic
        ResponseVO responseVO = new ResponseVO();
        SystemUser systemUser = LoginInterceptor.threadLocal.get();
        SystemUser userInfo = this.getById(systemUser.getId());
        if (userInfo != null) {
            responseVO.setStatusCode(200);
            responseVO.setMessage("获取用户信息成功");
            responseVO.setData(userInfo);
        } else {
            responseVO.setStatusCode(401);
            responseVO.setMessage("获取用户信息失败");
            responseVO.setData(null);
        }
        return responseVO; // 获取用户信息失败
    }

    @Override
    public ResponseVO logout() {
        // TODO: Implement logout logic
        SystemUser systemUser = LoginInterceptor.threadLocal.get();
        LoginInterceptor.threadLocal.remove();
        ResponseVO responseVO = new ResponseVO();
        responseVO.setStatusCode(200);
        responseVO.setMessage("登出成功");
        responseVO.setData(systemUser);
        return responseVO;
    }
}
